package com.nyway.wehos2r.client.content.music.player;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.allen_sauer.gwt.voices.client.Sound;
import com.allen_sauer.gwt.voices.client.SoundController;
import com.allen_sauer.gwt.voices.client.handler.PlaybackCompleteEvent;
import com.allen_sauer.gwt.voices.client.handler.SoundHandler;
import com.allen_sauer.gwt.voices.client.handler.SoundLoadStateChangeEvent;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.nyway.wehos2r.client.WeHostOurContext;
import com.nyway.wehos2r.client.content.music.SongDatabase.SongInfo;
import com.nyway.wehos2r.client.content.music.event.PlaySongEvent;
import com.nyway.wehos2r.client.content.music.event.PlaySongEventHandler;

public class MediaPlayer extends Composite implements PlaySongEventHandler {

	public static Logger logger = Logger.getLogger(MediaPlayer.class.getName());

	private static final Binder binder = GWT.create(Binder.class);

	// @UiField(provided = true)
	// PushButton playButton;

	@UiField(provided = true)
	HTML nowPlaying;

	private Sound currentSong;
	private final SoundController soundController = new SoundController();

	interface Binder extends UiBinder<Widget, MediaPlayer> {
	}

	public MediaPlayer() {
		logger.log(Level.INFO, "!MediaPlayer");
		nowPlaying = new HTML("hello");
		initWidget(binder.createAndBindUi(this));
		WeHostOurContext.EVENT_BUS
				.addHandler(PlaySongEvent.TYPE, this);

	}

	@Override
	public void onPlay(PlaySongEvent event) {
		logger.log(Level.INFO, "onPlay()");
		if (currentSong != null) {
			nowPlaying.setText("");
			currentSong.stop();
		}

		final SongInfo songInfo = event.getSongInfo();
		final Sound currentSong = soundController.createSound(
				Sound.MIME_TYPE_AUDIO_WAV_PCM, "/proxyfy?url="
						+ "http://www.memoclic.com/medias/sons-wav/2/714.wav",
				true, false);

		currentSong.addEventHandler(new SoundHandler() {

			@Override
			public void onSoundLoadStateChange(
					final SoundLoadStateChangeEvent event) {
				// simulate a slight variable delay for local development
				new Timer() {
					@Override
					public void run() {
						switch (event.getLoadState()) {
						case LOAD_STATE_SUPPORTED_AND_READY:
						case LOAD_STATE_SUPPORTED_NOT_READY:
						case LOAD_STATE_SUPPORTED_MAYBE_READY:
							currentSong.play();
							break;
						case LOAD_STATE_NOT_SUPPORTED:
							nowPlaying.setHTML("(sound or plugin unavailable)");
							break;
						case LOAD_STATE_SUPPORT_NOT_KNOWN:
							nowPlaying.setHTML("play (may not work)");
							break;
						case LOAD_STATE_UNINITIALIZED:
						default:
							throw new IllegalArgumentException(
									"Unhandled state " + event.getLoadState());
						}
					}
				}.schedule(Random.nextInt(500) + 200);
			}

			@Override
			public void onPlaybackComplete(PlaybackCompleteEvent event) {
				// TODO Auto-generated method stub

			}
		});
		nowPlaying.setHTML(// "<img class=\"loading-img\" src=\"1-0.gif\"/>"+
				"Loading song - " + songInfo.getName());
		// songPlaying(true);

	}

}
